//
//  HWTableViewCell.m
//  购物车
//
//  Created by 胡伟 on 16/10/10.
//  Copyright © 2016年 huwei. All rights reserved.
//

#import "HWTableViewCell.h"
#import "HWGoodsModel.h"
@implementation HWTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 95)];
        bgView.backgroundColor = [UIColor whiteColor];
        //商品图片
        _goodsImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 80, 80)];
        _goodsImage.image = [UIImage imageNamed:@"img6"];
        [bgView addSubview:_goodsImage];
        //添加商品标题
        _goodsTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 5, 200, 30)];
        [bgView addSubview:_goodsTitleLabel];
        //促销价
        _priceTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 35, 70, 30)];
        _priceTitleLabel.text = @"促销价:";
        [bgView addSubview:_priceTitleLabel];
        //商品价格
        _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 35, 100, 30)];
        [bgView addSubview:_priceLabel];
        //购买数量
        _goodsNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 65, 90, 30)];
        _goodsNumLabel.text = @"购买数量:";
        [bgView addSubview:_goodsNumLabel];
        //减
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.frame = CGRectMake(180, 65, 30, 30);
        [_deleteBtn setImage:[UIImage imageNamed:@"按钮-0"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _deleteBtn.tag = 11;
        [bgView addSubview:_deleteBtn];
        //购买商品的数量
        _numCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(210, 65, 50, 30)];
        _numCountLabel.textAlignment = NSTextAlignmentCenter;
        [bgView addSubview:_numCountLabel];
        //加
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(260, 65, 30, 30);
        [_addBtn setImage:[UIImage imageNamed:@"按钮+"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.tag = 12;
        [bgView addSubview:_addBtn];
        //是否选中图片
        _isSelectImage = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 60, 10, 30, 30)];
        [bgView addSubview:_isSelectImage];
        [self addSubview:bgView];
    }
    return self;
}
-(void)addTheValue:(HWGoodsModel*)goodsModel{
    _goodsTitleLabel.text = goodsModel.goodsTitle;
    _priceLabel.text = goodsModel.goodsPrice;
    _numCountLabel.text = [NSString stringWithFormat:@"%d",goodsModel.goodsNum];
    if (goodsModel.selectState) {
        _selectState = YES;
        _isSelectImage.image = [UIImage imageNamed:@"复选框-选中"];
    }else{
        _selectState = NO;
        _isSelectImage.image = [UIImage imageNamed:@"复选框-未选中"];
    }
}
//减点击事件
-(void)deleteBtnAction:(UIButton*)sender{
    //判断是选中  选中才能点击
    if (_selectState == YES) {
        //调用代理
        [self.delegate btnClick:self andfFlag:(int)sender.tag];
    }
}
//加点击事件
-(void)addBtnAction:(UIButton*)sender{
    //判断是选中  选中才能点击
    if (_selectState == YES) {
        //调用代理
        [self.delegate btnClick:self andfFlag:(int)sender.tag];
    }
}

@end
